<?php

namespace AdminModule;

class DepartmentPresenter extends \BasePresenter
{

	public function renderDefault()
	{
		$departmentModel = $this->context->models->department;
		$this->template->departments = $departmentModel->getDepartments();
	}

	public function renderAddDepartment()
	{
		
	}

	public function renderDepartment($id)
	{
		$departmentModel = $this->context->models->department;
		$department = $departmentModel->getDepartment(array('id' => $id));
		$this['editDepartmentForm']->setValues($department);
		
		$this->template->id = $id;
		$this->template->department = $department['name'];
		$this->template->skills = $departmentModel->getSkills(array('department_id'=>$id));
		
	}
	
	public function renderAddSkill($department)
	{
		$this['addSkillForm']['department']->setValue($department);
	}
	
	public function renderEditSkill($id)
	{
		$departmentModel = $this->context->models->department;
		$skill = $departmentModel->getSkill(array('id'=>$id));
		$this->template->skill = $skill['name'];
		$this['editSkillForm']->setValues($skill);
	}
	
	/** @secured */
	public function handleDeleteDepartment($id)
	{
		$departmentModel = $this->context->models->department;
		if($departmentModel->deleteDepartment($id)){
			$this->flashMessage('Oddělení bylo úspěšně smazáno');
			$this->redirect('this');
		}else{
			$this->flashMessage('Chyba při mazání oddělení. Oddělení může být smazáno pouze pokud neobsahuje žádné dovednosti.','error');
		}
	}
	
	/** @secured */
	public function handleDeleteSkill($id)
	{
		$departmentModel = $this->context->models->department;
		$skill = $departmentModel->getSkill(array('id'=>$id));
		if($departmentModel->deleteSkill($id)){
			$this->flashMessage('Dovednost byla úspěšně smazána');
		}else{
			$this->flashMessage('Chyba při mazání dovednosti. Nelze smazat dovednost přiřazenou uživateli.','error');
		}
		$this->redirect('department',$skill['department_id']);
	}

	public function createComponentAddDepartmentForm($name)
	{
		$form = new \Nette\Application\UI\Form($this, $name);
		$form->addText('name', 'Název:');
		$form->addSubmit('send', 'Uložit');
		$form->onSuccess[] = callback($this, 'addDepartment');
		return $form;
	}

	public function createComponentEditDepartmentForm($name)
	{
		$form = new \Nette\Application\UI\Form($this, $name);
		$form->addText('name', 'Název:');
		$form->addHidden('id');
		$form->addSubmit('send', 'Uložit');
		$form->onSuccess[] = callback($this, 'editDepartment');
		return $form;
	}

	public function createComponentAddSkillForm($name)
	{
		$form = new \Nette\Application\UI\Form($this, $name);
		$form->addText('name', 'Název:');
		$form->addHidden('department');
		$form->addSubmit('send', 'Uložit');
		$form->onSuccess[] = callback($this, 'addSkill');
		return $form;
	}
	
	public function createComponentEditSkillForm($name)
	{
		$form = new \Nette\Application\UI\Form($this, $name);
		$form->addText('name', 'Název:');
		$form->addHidden('department_id');
		$form->addHidden('id');
		$form->addSubmit('send', 'Uložit');
		$form->onSuccess[] = callback($this, 'editSkill');
		return $form;
	}
	
	public function addDepartment($form)
	{
		$values = $form->values;
		$departmentModel = $this->context->models->department;
		$department = $departmentModel->addDepartment($values);
		if($department !== false){
			$this->flashMessage('Oddělení bylo úspěště přidáno');
			$this->redirect('department', $department['id']);
		}else{
			$this->flashMessage('Chyba při vytváření oddělení.','error');
		}
	}

	public function editDepartment($form)
	{
		$values = $form->values;
		$departmentModel = $this->context->models->department;
		if($departmentModel->editDepartment($values) !== false){
			$this->flashMessage('Oddělení bylo úspěšně upraveno');
			$this->redirect('default');
		}else{
			$this->flashMessage('Chyba při úpravě oddělení.','error');
		}
	}
	
	public function addSkill($form)
	{
		$values = $form->values;
		$departmentModel = $this->context->models->department;
		if($departmentModel->addSkill($values['name'],$values['department'])){
			$this->flashMessage('Dovednost byla úspěšně přidána');
			$this->redirect('department', $values['department']);
		}else{
			$this->flashMessage('Chyba při ukládání dovednosti.','error');
		}
	}

	public function editSkill($form)
	{
		$values = $form->values;
		$departmentModel = $this->context->models->department;
		if($departmentModel->editSkill($values) !== false){
			$this->flashMessage('Dovednost byla úspěšně upravena');
			$this->redirect('department', $values['department_id']);
		}else{
			$this->flashMessage('Chyba při ukládání dovednosti.','error');
		}
	}

}